﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Abreu.Profile.ObjectClasses;
using Abreu.Profile.Database;
using System.Data.Linq;


namespace CorpAbreu.Common.Models
{
    public class OrganizationDataSource
    {
        #region Class Members
        private CorpAbreuDataContext _corpAbreuDC = new CorpAbreuDataContext();
        private GeneralDataSource gnDS = new GeneralDataSource();
        #endregion

        #region Public Members
        /// <summary>
        /// Gets the abreu locations that matches the search parameters
        /// </summary>
        /// <param name="name"></param>
        /// <param name="local"></param>
        /// <param name="countryId"></param>
        /// <param name="distritoId"></param>
        /// <param name="cityId"></param>
        /// <param name="concelhoId"></param>
        /// <param name="status"></param>
        /// <returns>List of AbreuLocationInfo class</returns>
        public List<AbreuLocationInfo> SearchAbreuLocation(String name, String local, int? countryId, int? distritoId,
                                                        int? cityId, int? concelhoId, String nuts3, String status)
        {
            List<AbreuLocationInfo> abreuResults = new List<AbreuLocationInfo>();

            //Get search results with P_SearchAbreuLocation_Sel
            var objSearchResult = _corpAbreuDC.P_SearchAbreuLocation(name, local, countryId, distritoId, cityId,
                                                                concelhoId, nuts3, status);

            foreach (P_SearchAbreuLocationResult objSearchItem in objSearchResult)
            {

                AbreuLocationInfo abrRes = new AbreuLocationInfo(objSearchItem.Abreulocationid, objSearchItem.Name, objSearchItem.Address, objSearchItem.Local,
                                                 objSearchItem.PostalCode, objSearchItem.PostalLocal, objSearchItem.Email, objSearchItem.Phone, objSearchItem.Fax,
                                                 gnDS.GetCityById(objSearchItem.CityId), objSearchItem.CityId, gnDS.GetCountryById(objSearchItem.CountryId), objSearchItem.CountryId,
                                                 gnDS.GetDistritoById(objSearchItem.DistritoId), objSearchItem.DistritoId, gnDS.GetConcelhoById(objSearchItem.ConcelhoId, objSearchItem.DistritoId),
                                                 objSearchItem.ConcelhoId, objSearchItem.Status, objSearchItem.Version,
                                                 gnDS.GetNutById(objSearchItem.Nuts3), objSearchItem.Nuts3);
                abreuResults.Add(abrRes);
            }

            return abreuResults;
        }

        /// <summary>
        /// Gets the abreu location details by locationId
        /// </summary>
        /// <param name="LocationId"></param>
        /// <returns>List of AbreuLocationInfo class</returns>
        public AbreuLocationInfo GetLocationInfo(int locationId)
        {
            AbreuLocationInfo abreuLocation = new AbreuLocationInfo();

            //Get Location with P_AbreuLocationById_Sel
            var objLocation = _corpAbreuDC.P_GetAbreuLocationById(locationId);

            foreach (P_GetAbreuLocationByIdResult objLocationItem in objLocation)
            {
                abreuLocation = new AbreuLocationInfo(objLocationItem.ABREULOCATIONID, objLocationItem.NAME, objLocationItem.ADDRESS, objLocationItem.LOCAL,
                                                                objLocationItem.POSTALCODE, objLocationItem.POSTALLOCAL, objLocationItem.EMAIL, objLocationItem.PHONE,
                                                                objLocationItem.FAX, objLocationItem.CITYID, objLocationItem.COUNTRYID, objLocationItem.DISTRITOID,
                                                                objLocationItem.CONCELHOID, objLocationItem.STATUS, objLocationItem.VERSION, objLocationItem.Nuts3);
            }

            return abreuLocation;
        }

        /// <summary>
        /// Updates the abreu location info by id
        /// </summary>
        /// <param name="locationId"></param>
        /// <param name="address"></param>
        /// <param name="local"></param>
        /// <param name="postLocal"></param>
        /// <param name="postCode"></param>
        /// <param name="email"></param>
        /// <param name="phone"></param>
        /// <param name="fax"></param>
        /// <param name="countryId"></param>
        /// <param name="districtId"></param>
        /// <param name="concelId"></param>
        /// <param name="cityId"></param>
        /// <param name="state"></param>
        /// <returns>int</returns>
        public int UpdateAbreuLocation(int locationId, string name, string address, string local, string postLocal,
                                    string postCode, string email, string phone, string fax, int? countryId,
                                    int? districtId, int? concelId, int? cityId, string nuts3, int userId, 
                                    string state, Binary version)
        {
            int returnValue = _corpAbreuDC.p_AbreuLocation_upd(locationId, name, address, local, postCode,
                                                               postLocal, email, phone, fax, cityId, countryId,
                                                               districtId, concelId, nuts3, userId, DateTime.Now, version);

            return returnValue;
        }

        /// <summary>
        /// Insert new Abreu Location
        /// </summary>
        /// <param name="name"></param>
        /// <param name="address"></param>
        /// <param name="local"></param>
        /// <param name="postCode"></param>
        /// <param name="postLocal"></param>
        /// <param name="email"></param>
        /// <param name="phone"></param>
        /// <param name="fax"></param>
        /// <param name="cityId"></param>
        /// <param name="countryId"></param>
        /// <param name="districtId"></param>
        /// <param name="concelId"></param>
        /// <param name="userId"></param>
        /// <param name="state"></param>
        /// <returns>Location Id int</returns>
        public int? InsertAbreuLocation(string name, string address, string local, string postCode,
                                    string postLocal, string email, string phone, string fax, int? cityId,
                                    int? countryId, int? districtId, int? concelId, string nuts3, int userId,
                                    string state)
        {
            int? returnValue = 0;

            _corpAbreuDC.p_AbreuLocation_ins(name, address, local, postCode,
                                             postLocal, email, phone, fax, cityId, countryId,
                                             districtId, concelId, nuts3, userId, DateTime.Now, ref returnValue);

            return returnValue;
        }

        /// <summary>
        /// Gets the new version created in Db with the update
        /// </summary>
        /// <param name="locationId"></param>
        /// <returns>Binary</returns>
        public Binary GetLocationVersionById(int locationId)
        {
            Binary version = null;
            var returnValue = _corpAbreuDC.P_GetLocationVersionById(locationId);

            foreach (P_GetLocationVersionByIdResult retVal in returnValue)
            {
                version = retVal.Version;
            }

            return version;
        }
        #endregion
    }
}
